package com.shr.xsunmvc.servlet.packer;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

public class Request {

	private HttpServletRequest request;
	
	public Request(HttpServletRequest servletRequest) {
		this.request = servletRequest ; 
	}
	
	public HttpServletRequest getRequest(){
		return request ;
	}
	
	public void set(String key, Object value){
		request.setAttribute(key, value) ;
	}
	
	@SuppressWarnings("unchecked")
	public <T>T get(String key){
		T value = (T) request.getAttribute(key) ;
		if(value != null)
			return value ;
		return null ;
	}
	
	public String getCookie(String key){
		Cookie[] cookies = request.getCookies() ;
		if(cookies != null && cookies.length > 0){
			for(Cookie cookie : cookies){
				if(cookie.getName().equals(key))
					return cookie.getValue() ;
			}
		}
		return null ;
	}

}
